package com.controller;

import com.entity.RbacManager;
import com.entity.RbacRole;
import com.entity.ResponseEntity;
import com.service.RbacManagerService;
import com.service.RbacRoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/rbacManager")
public class RbacManagerController {
    private RbacManagerService rbacManagerService;
    private RbacRoleService rbacRolesService;
    @Autowired
    public void setRbacManagerService(RbacManagerService rbacManagerService) {
        this.rbacManagerService = rbacManagerService;
    }
    @Autowired
    public void setRbacRolesService(RbacRoleService rbacRolesService) {
        this.rbacRolesService = rbacRolesService;
    }

    @GetMapping("/findAll/{currentPage}/{pageSize}")
    public ResponseEntity<RbacManager> findAll(@PathVariable int currentPage,@PathVariable int pageSize){
        return  rbacManagerService.findAll(currentPage,pageSize);
    }
    //删除
    @DeleteMapping("/deleteOne/{id}")
    public ResponseEntity<RbacManager> deleteOne(@PathVariable int id){
        return rbacManagerService.deleteOne(id);
    }
    //添加用户
    @PostMapping("/addOne")
    public ResponseEntity<RbacManager> addOne(@RequestBody RbacManager rbacManager){
        return rbacManagerService.addOne(rbacManager);
    }
    //修改用户
    @PutMapping("/updateOne")
    public ResponseEntity<RbacManager> updateOne(@RequestBody RbacManager rbacManager){
        return rbacManagerService.updateOne(rbacManager);
    }

    //所有的用户角色
    @GetMapping("/findAllRoles")
    public ResponseEntity<RbacRole> findAllRoles(){
        return rbacRolesService.findAll();
    }

    @GetMapping("search/{account}/{status}")
    public ResponseEntity<RbacManager> search(@PathVariable String account,@PathVariable String status ){
        return rbacManagerService.search(account,status);
    }
//    @GetMapping("search/{account}")
//    public ResponseEntity<RbacManager> search(@PathVariable String account){
//        return rbacManagerService.search(account,"");
//    }

    //需要一个角色，才允许访问我的controller
    @RequestMapping("/testRole")
    @RequiresRoles("普通员工")
    public ResponseEntity<RbacManager> testRole(){
        return new ResponseEntity<RbacManager>(200,"拥有普通员工的权限",null);
    }

    @RequestMapping("/testPerm")
    //需要一个角色，才能访问controller
    @RequiresPermissions(value = {"perm:list","perm,update"},logical = Logical.OR)
    public ResponseEntity<RbacManager> testPerm(){
        return new ResponseEntity<RbacManager>(200,"拥有list，或者update权限",null);
    }
}
